package orma

import (
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
)

var (
	models = make([]XTransaction, 0)
	engine *xorm.Engine
)

func GetEngine() *xorm.Engine {
	return engine
}

//Register model init 调用
func Register(model XTransaction) {
	models = append(models, model)
}

func Sync() (err error) {
	for _, val := range models {
		if err = engine.Sync2(val); err != nil {
			return
		}
		iBeanA, ok := val.(XInitData)
		if ok {
			n, e := engine.Count(iBeanA)
			if e != nil {
				err = e
				return
			}
			if n == 0 {
				_, err = engine.Insert(iBeanA.InitData())
				if err != nil {
					return
				}
			}
		}
	}
	return
}
